//
//  MainViewController.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/7/27.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setupComposeButton()
    }
    
    //添加控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoveryTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    //设置控制器的相关属性
    private func addChildViewController(vc:UITableViewController, title: String, imageName: String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

    private func setupComposeButton() {
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        composeButton.frame = CGRectMake(w * 2, 0, w, tabBar.bounds.height)
    }
    
    func clickComposedButton() {
        let vc = UIViewController()
        let nav = UINavigationController(rootViewController: vc)
        vc.view.backgroundColor = UIColor.redColor()
        
        presentViewController(nav, animated: true) { () -> Void in
            print("发微博")
        }
        
    }
    
    //懒加载Button
    lazy private var composeButton: UIButton = {
        
        let button = UIButton()
       // button.backgroundColor = UIColor.redColor()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        button.addTarget(self, action:"clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()

}
